package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mqx
 * @date 2020-12-12 10:18:07
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;
    //  http://list.gmall.com/list.html?category3Id=61  http://list.gmall.com/list.html?keyword=小米手机
    @RequestMapping("list.html")
    public String list(SearchParam searchParam, Model model){
        //  获取数据
        Result<Map> result = listFeignClient.list(searchParam);

        //  ${urlParam} urlParam 记录查询条件
        String urlParam = this.makeUrlParam(searchParam);

        //  ${trademarkParam} 存储品牌的数据
        String trademarkParam =  this.makeTrademark(searchParam.getTrademark());

        //  ${propsParamList} 存储平台属性 这个集合可以看作一个对象，也可以看作一个map
        //  如果是对象的话 prop.attrName = 值 ， prop.attrValue = 值  |  map.put("attrName","value")

        //  List<SearchAttr> propsParamList = xxx(searchParam.getProps());
        List<Map<String,Object>> propsParamList = this.makeProps(searchParam.getProps());

        //  获取排序规则
        //  ${orderMap.type == '1' ? 'active': ''}  ${orderMap.sort == 'asc' ? 'desc' : 'asc'}
        HashMap<String,String> orderMap = this.dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("urlParam",urlParam);
        //  存储数据
        model.addAllAttributes(result.getData());
        //  searchParam
        model.addAttribute("searchParam",searchParam);

        return "list/index";
    }

    //  第一次点击 ：order=1:asc  第二次点击 order=1:desc
    private HashMap<String, String> dealOrder(String order) {
        HashMap<String, String> hashMap = new HashMap<>();
        //  判断
        if(!StringUtils.isEmpty(order)){
            //  分割字符串
            String[] split = order.split(":");
            if (split!=null && split.length==2){
                hashMap.put("type",split[0]);
                hashMap.put("sort",split[1]);
            }else {
                hashMap.put("type","1");
                hashMap.put("sort","desc");
            }
        }else {
            hashMap.put("type","1");
            hashMap.put("sort","desc");
        }
        return hashMap;
    }

    //  平台属性的面包屑制作
    private List<Map<String, Object>> makeProps(String[] props) {

        List<Map<String, Object>> list = new ArrayList<>();
        //  &props=106:安卓手机:手机一级&props=107:小米:二级手机
        if (props!=null && props.length>0){
            //  循环遍历
            for (String prop : props) {
                //  每个prop = 106:安卓手机:手机一级
                //  获取平台属性名，平台属性值名，平台属性Id
                String[] split = prop.split(":");
                if (split!=null && split.length==3){
                    Map<String, Object> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    //  将map 添加到集合
                    list.add(map);
                }
            }
        }
        return list;
    }

    //  获取品牌的面包屑
    private String makeTrademark(String trademark) {
        //  &trademark=2:苹果
        if (!StringUtils.isEmpty(trademark)){
            //  字符串分割
            String[] split = trademark.split(":");
            //  判断数据
            if (split!=null && split.length==2){
                //  获取品牌名称
                return "品牌:" + split[1];
            }
        }
        return null;
    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        //  判断用户第一次根据什么查询的
        //  http://list.gmall.com/list.html?keyword=小米手机
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            sb.append("keyword=").append(searchParam.getKeyword());
        }
        //  http://list.gmall.com/list.html?category3Id=61
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            sb.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            sb.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            sb.append("category1Id=").append(searchParam.getCategory1Id());
        }

        //  品牌
        //  http://list.gmall.com/list.html?category3Id=61&trademark=4:小米
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            if (sb.length()>0){
                sb.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        //  平台属性
        //  http://list.gmall.com/list.html?category3Id=61&trademark=4:小米&props=23:8G:运行内存33&props=106:安卓手机:手机一级
        String[] props = searchParam.getProps();
        if (props!=null && props.length>0){
            //  循环遍历
            for (String prop : props) {
                //  这prop 相当一单个的属性值查询
                if (sb.length()>0){
                    sb.append("&props=").append(prop);
                }
            }
        }
        //  返回查询条件
        return "list.html?"+sb.toString();
    }
}
